package com.mtt.erp.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mtt.erp.common.exception.MttErpException;
import com.mtt.erp.common.utils.MttErpUtil;
import com.mtt.erp.logistics.domain.OrderFaceTemplete;
import com.mtt.erp.logistics.dao.OrderFaceTempleteMapper;
import com.mtt.erp.logistics.dto.OrderFaceTempSaveDto;
import com.mtt.erp.logistics.service.IOrderFaceTempleteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author MrBird
 */
@Service
@Slf4j
public class OrderFaceTempleteServiceImpl extends ServiceImpl<OrderFaceTempleteMapper, OrderFaceTemplete> implements IOrderFaceTempleteService {

    @Value("${erp.max.batch.insert.num}")
    private int batchInsertMaxNum;

    @Override
    @Transactional
    public Boolean saveOrderFaceTemplete(OrderFaceTempSaveDto orderFaceTempSaveDto) throws MttErpException {
        //保存面单数据到数据表中
        OrderFaceTemplete orderFaceTemplete= OrderFaceTemplete.builder().build();
        BeanUtils.copyProperties(orderFaceTempSaveDto,orderFaceTemplete);
        orderFaceTemplete.setCdate(LocalDateTime.now());
        Long currentUserId = MttErpUtil.getCurrentUser().getUserId();//面单数据是哪一个用户保存的
        orderFaceTemplete.setCuser(currentUserId.toString());
        try {
            return this.save(orderFaceTemplete);
        }catch (Exception e){
            log.error("保存面单失败");
            throw new MttErpException("保存面单失败");
        }
    }

    @Override
    public OrderFaceTemplete findByOrderCode(String orderCode) {
        LambdaQueryWrapper<OrderFaceTemplete> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderFaceTemplete::getOrderCode, orderCode);
        return this.getOne(queryWrapper);
    }


}
